//
//  YFMineTeacherViewController.m
//  YFListenStock
//
//  Created by ChinaRainbow-iOS on 2018/10/17.
//  Copyright © 2018年 ChinaRainbow-iOS. All rights reserved.
//

#import "YFMineViewController.h"
#import "YFUserModel.h"
#import "YFBaseTableViewCell.h"
#import "YFLoginRegistViewController.h"
#import "YFVoice60ListViewController.h"
#import "YFMineViewModel.h"
#define ot_imageHeight  WINDOW_WIDTH/375.0*187.0

@interface YFMineViewController ()
@property (nonatomic, strong) YFUserModel * otherModel;
@property (strong, nonatomic) UIButton *rightBtn;
@property (strong, nonatomic) UIImageView *ot_headBackImageView;
@property (strong, nonatomic) UIImageView *ot_headImageView;
@property (strong, nonatomic) UILabel *ot_nickLabel;
@property (strong, nonatomic) UIButton *ot_sexBtn;
@property (nonatomic, strong) UIView *ot_headerBackView;
@property (nonatomic, strong) NSMutableArray *imageArray;

@end

@implementation YFMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.fd_prefersNavigationBarHidden = YES;
    self.dataArray = [NSMutableArray array];
    self.imageArray = [NSMutableArray array];

    [self.dataArray safeAddObj: @[@"我的订单",@"我的收藏",@"我的关注",@"我的评论"]];
    [self.dataArray safeAddObj: @[@"设置"]];
    [self.imageArray safeAddObj: @[@"mine_teach_order",@"mine_teach_collection",@"mine_teach_like",@"mine_teach_comments"]];
    [self.imageArray safeAddObj: @[@"mine_teach_setting"]];
    
    [self setUpUI];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView.mj_header beginRefreshing];
    self.tableView.backgroundColor = HexColor(0xf7f8f9);
    self.emptyBackgroundColor = [UIColor clearColor];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUserInfo) name:kLoginSuccess object:nil];
    [self updateUserInfo];
}
-(void)updateUserInfo{
    [YFMineViewModel request_getUserInfoComplete:^(NSDictionary *error, NSDictionary *result) {
        if (result) {
            NSMutableDictionary *dict = [result mutableCopy];
            dict[@"token"] = [YFUserModel shareModel].token;
            [YFUserModel model:[YFUserModel shareModel] setValuesForKeys:dict];
            [YFUserModel saveUserInfo:dict];
        }
    } ];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
-(void)setUpUI{
    self.view.backgroundColor = HexColor(0xf5f6f8);
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.mas_equalTo( - 5);
    }];
    self.rightBtn = ({
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:btn];
        [btn setImage:[UIImage imageNamed:@"mine_teach_message"] forState:0];;
        btn.titleLabel.font = [UIFont zg_systemFontWithSize:14];
        [btn showBadgeWithStyle:WBadgeStyleNumber value:12 animationType:WBadgeAnimTypeNone];
        btn.badgeCenterOffset = CGPointMake(25, 10);
        [btn resumeBadge];
        btn.badgeBgColor = [UIColor orangeColor];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo([YFUITool ajustTopEdgeOnIphoneX:20]);
            make.height.mas_equalTo(40);
            make.right.mas_equalTo(-15);
            make.width.mas_equalTo(40);
        }];
        
        btn;
    });
    [self.rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //添加头像图片
    [self addHeaderImageView];
    
}
-(void)rightBtnClick{
    YFLoginRegistViewController *vc = [[YFLoginRegistViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

//添加头像
-(void)addHeaderImageView{
    [self.tableView setTableHeaderView:self.ot_headerBackView];
    [self.ot_headerBackView addSubview:self.ot_headBackImageView];
    [self.ot_headerBackView addSubview:self.ot_headImageView];
    [self.ot_headerBackView addSubview:self.ot_nickLabel];
    [self.ot_headerBackView addSubview:self.ot_sexBtn];
    [self.ot_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(34);
        make.bottom.mas_equalTo(-55);
        make.height.width.mas_equalTo(66);
    }];
    self.ot_headImageView.layer.cornerRadius = 66.0/2.0;
    self.ot_headImageView.backgroundColor = [UIColor clearColor];
    
    [self.ot_nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.ot_headImageView.mas_right).offset(11);
        make.bottom.equalTo(self.ot_headImageView.mas_bottom).offset(-10);
        make.height.mas_equalTo(12);
    }];
    
    [self.ot_sexBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.ot_headImageView.mas_right).offset(11);
        make.bottom.equalTo(self.ot_headImageView.mas_bottom).offset(-40);
        make.height.mas_equalTo(12);
    }];
    [self.ot_sexBtn addTarget:self action:@selector(editInfo) forControlEvents:UIControlEventTouchUpInside];
    self.ot_sexBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
}
-(void)updateInfoWithModel:(YFUserModel *)tempModel{
//    if (tempModel) {
//        self.model = tempModel;
//    }
//    [self.ot_sexBtn setTitle:[NSString stringWithFormat:@"%@",self.model.nick_name.length > 0? self.model.nick_name:@"昵称"] forState:0];;
//    self.ot_nickLabel.text = [NSString stringWithFormat:@"%@",self.model.signature.length > 0? self.model.signature:@""] ;
//    if ([self.model.Id integerValue] == [[ATUserModel shareModel].Id integerValue]) {
//        [self.ot_sexBtn setImage:[UIImage imageNamed:@"mine_edit"] forState:0];
//    }
//    [self.ot_sexBtn ajustImagePosition:ButtonImagePosition_Right offset:10];
//
//    self.ot_headImageView.image = [ATUserModel getUserheadImage:self.model.sex];
}
-(void)editInfo{
    
}
//滚动tableview 完毕之后
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //图片高度
    CGFloat imageHeight = self.ot_headerBackView.frame.size.height;
    //图片宽度
    CGFloat imageWidth = WINDOW_WIDTH;
    //图片上下偏移量
    CGFloat imageOffsetY = scrollView.contentOffset.y;
    //上移
    if (imageOffsetY < 0) {
        CGFloat totalOffset = imageHeight + ABS(imageOffsetY);
        CGFloat f = totalOffset / imageHeight;
        self.ot_headBackImageView.frame = CGRectMake(-(imageWidth * f - imageWidth) * 0.5, imageOffsetY, imageWidth * f, totalOffset);
    }
}

#pragma mark tableview delegate datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array = [self.dataArray safeObjectWithIndex:section];
    return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID2 = @"cellID2";
    YFBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID2];
    if (cell  == nil) {
        cell = [[YFBaseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID2];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        [cell updateSepaLineWithLeft:0 right:0];
        cell.textLabel.font = [UIFont zg_systemFontWithSize:12];
    }
    cell.imageView.image = [UIImage imageNamed:[[self.imageArray safeObjectWithIndex:indexPath.section] safeObjectWithIndex:indexPath.row]];
    cell.textLabel.text = [[self.dataArray safeObjectWithIndex:indexPath.section] safeObjectWithIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *headerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, 5+87)];
        headerV.backgroundColor = [UIColor zg_f7f8f9BackGrayColor];
        UIView *lineVV = [YFUITool zg_createLineView];
        lineVV.frame = CGRectMake(0, 4.5+87, WINDOW_WIDTH, 0.5);
        [headerV addSubview:lineVV];
        
        UIButton *leftBtn = [self createButtonWithTltle:@"我的语音60s" imageName:@"mine_teacher_vioce60" rect:CGRectMake(0, 0, WINDOW_WIDTH/3.0, 87)];
        [leftBtn block:^(id sender) {
            NSLog(@"left");
            YFVoice60ListViewController *vc = [[YFVoice60ListViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }];
        [headerV addSubview:leftBtn];

        UIButton *middleBtn = [self createButtonWithTltle:@"我的专家解读" imageName:@"mine_teach_zhuanjiajiedu" rect:CGRectMake(WINDOW_WIDTH/3.0, 0, WINDOW_WIDTH/3.0, 87)];
        [middleBtn block:^(id sender) {
            NSLog(@"middleBtn");
        }];
        [headerV addSubview:middleBtn];
        UIButton *rightBtn = [self createButtonWithTltle:@"我的直播间" imageName:@"mine_teach_zhibojian" rect:CGRectMake(WINDOW_WIDTH/3.0*2.0, 0, WINDOW_WIDTH/3.0, 87)];
        [rightBtn block:^(id sender) {
            NSLog(@"right");
        }];
        [headerV addSubview:rightBtn];
        return headerV;
    }
    UIView *headerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, 5)];
    headerV.backgroundColor = [UIColor zg_f7f8f9BackGrayColor];
    UIView *lineVV = [YFUITool zg_createLineView];
    lineVV.frame = CGRectMake(0, 4.5, WINDOW_WIDTH, 0.5);
    [headerV addSubview:lineVV];
    return headerV;
}
-(UIButton *)createButtonWithTltle:(NSString *)title imageName:(NSString *)imageName rect:(CGRect)rect{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:0];
    [button setTitle:title forState:UIControlStateHighlighted];
    [button setTitleColor:HexColor(0x313439) forState:0];
    button.titleLabel.font = [UIFont zg_systemFontWithSize:11];
    button.frame = rect;
    [button setImage:[UIImage imageNamed:imageName] forState:0];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
    [button ajustImagePosition:ButtonImagePosition_Top offset:6];
    button.backgroundColor = [UIColor whiteColor];
    return button;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 5+87;
    }
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 48;
}

#pragma mark - 懒加载
-(UIView *)ot_headerBackView{
    if (_ot_headerBackView == nil) {
        _ot_headerBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, ot_imageHeight)];
        [_ot_headerBackView setBackgroundColor:[UIColor lightGrayColor]];
    }
    return _ot_headerBackView;
}
-(UIImageView *)ot_headBackImageView{
    if (_ot_headBackImageView == nil) {
        _ot_headBackImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, ot_imageHeight)];
        [_ot_headBackImageView setBackgroundColor:[UIColor whiteColor]];
        _ot_headBackImageView.image = [UIImage imageNamed:@"mine_teachBg"];
        [_ot_headBackImageView setContentMode:UIViewContentModeScaleAspectFill];
        [_ot_headBackImageView setClipsToBounds:YES];
    }
    return _ot_headBackImageView;
}
-(UIImageView *)ot_headImageView{
    if (_ot_headImageView == nil) {
        _ot_headImageView = [[UIImageView alloc] init];
        [_ot_headImageView setClipsToBounds:YES];
        _ot_headImageView.image = [UIImage imageNamed:@"mine_teach_defaultIcon"];
    }
    return _ot_headImageView;
}
-(UILabel *)ot_nickLabel{
    if (_ot_nickLabel == nil) {
        _ot_nickLabel = [[UILabel alloc]init];
        _ot_nickLabel.textColor = [UIColor blackColor];
        _ot_nickLabel.font = [UIFont zg_systemFontWithSize:11];
        _ot_nickLabel.text = @"会飞的思想狂魔";
    }
    return _ot_nickLabel;
}

-(UIButton *)ot_sexBtn{
    if (_ot_sexBtn == nil) {
        _ot_sexBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_ot_sexBtn setTitleColor:[UIColor blackColor] forState:0];
        _ot_sexBtn.titleLabel.font = [UIFont zg_systemFontWithSize:17];
        [_ot_sexBtn setTitle:@"关注 26  粉丝 4" forState:0];
        [_ot_sexBtn setImage:[UIImage imageNamed:@"mine_edit"] forState:0];
    }
    return _ot_sexBtn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
